import encrypt from "sm-crypto";

export default class Sm3Test {
  objSerialize(obj: any) {
    if (!obj) return obj;
    let objKeys = Object.keys(obj).sort();
    let tempObj = {};
    objKeys.forEach((item) => {
      Object.assign(tempObj, { [item]: obj[item] });
    });
    return tempObj;
  }

  /**
  生成签名
  */
  getSign(baseParms: any) {
    const sm3 = encrypt.sm3;
    // 秘钥
    const secret = "fldPafds2(0+1=3)";

    // const timestamp = Date.now(); // 时间戳为毫秒级
    // 1.将对象进行排序序列化
    const parms = this.objSerialize(baseParms);
    // 2.将排序好的对象后面再追加一个secret秘钥字段
    const parmsStr = JSON.stringify({
      ...parms,
      secret: secret
    });
    //3.加密的sm3字符串需要转成大写
    return sm3(parmsStr).toUpperCase();
  }

  main() {
    console.log("进入 Sm3Test 测试");
    var timestamp = Date.now();
    timestamp = 1684404982732;
    const baseParms = {
      page: '1',
      size: '10',
      t: '1684404982732'
    };
    var ss1="914FE38FC571BCB4D325C147C82FFE9745675517BC878EB45AF68B880297E52A";
    var ss2='914FE38FC571BCB4D325C147C82FFE9745675517BC878EB45AF68B880297E52A';
    
    var sign = this.getSign(baseParms);
    console.log(sign);
  }
}
